package me.wxk.interview.activemq.service;

import me.wxk.interview.activemq.message.Email;
import org.springframework.stereotype.Service;

import javax.jms.*;
import java.io.Serializable;

/**
 * @author wangxuekai
 * @version 2.3
 * @Date 2017/5/11.
 * @since 2.3
 */
@Service("messageListener")
public class JmsQueueMessageListener implements MessageListener {

    public void onMessage(Message message) {
        System.out.println("收到消息:" + message);
        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            try {
                String text = textMessage.getText();
                int i = 1 / 0;
                System.out.println("消费成功:[" + text + "]");
            } catch (JMSException e) {
                e.printStackTrace();
            }
        } else if (message instanceof ObjectMessage) {
            ObjectMessage objectMessage = (ObjectMessage) message;
            try {
                Email obj = (Email) objectMessage.getObject();
                System.out.println("收到邮件:" + obj);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
